<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class AppCoursePage extends Model
{
    use HasFactory;
    const STATUS = [0=>'下架',1=>'上架'];
    const TYPE = [0=>'首页',1=>'微页面'];
    protected $appends = [
        'status_text','type_text'
    ];

    protected function serializeDate(\DateTimeInterface $date)
    {
        return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
    }

    public function getContentAttribute(string $value)
    {
        $course = AppCourse::select('id','title','cover','price','original_price','status','learns')->withCount('orders')->get();
        $data = json_decode($value);
        foreach ($data as $k => $v) {
            if($v->type == 'shop'){
                foreach ($v->data as $kk => $vv) {
                    $v->data[$kk] = $course->where('id',$vv)->first();
                }
            }
        }
        return $data;
    }

    /**
     *  获取状态.
     *
     * @param string $value
     * @return string
     */
    public function getStatusTextAttribute()
    {
        $status = $this->attributes['status'];
        return self::STATUS[$status];
    }

    /**
     *  获取类型.
     *
     * @param string $value
     * @return string
     */
    public function getTypeTextAttribute()
    {
        $type = $this->attributes['type'];
        return self::TYPE[$type];
    }

}
